#coding:utf-8
'''
create on 2017-04-20
@author:sandy
'''
import logging
from pycommon import Const
from pycommon import Pack as packet
from pycommon import Utils
import HallManager
import ClientFactory
import UserManager
import CenterBuild
import ClientBuild
import Game


def cmdKeepAlive(center,pkt):
    logging.debug(u"center--->gate keepAlive")
    pass

def cmdLoginCenter(center,pkt):
    logging.debug("game->center login sucess")
    pass

def cmdCreateTable(center,pkt):
    queneId,uid,maxRound,hallId,playModel,tableIdStr = packet.unpack("HIHHHs",pkt)
    logging.debug("cmdCreateTable uid=%d maxRound=%d hallId=%d playModel=%d tableIdStr=%s",uid,maxRound,hallId,playModel,tableIdStr)
    code = Const.ERR_OK
    hall = HallManager.instance.getHall(hallId)
    if hall == None:
        code = Const.ERR_HALL_NOT_EXIST
    else:
        table = hall.getNewTalbe()
        if table == None:
            code = Const.ERR_NO_TABLE
        else:
            table.setTableInfo(uid,hallId,playModel,maxRound,tableIdStr)
    center.sendPacket(CenterBuild.createTable(code,queneId,hallId,table.getId()))
    pass

def cmdCheckUser(center, pkt):
    code,uid,pid = packet.unpack("HIH",pkt)
    client = ClientFactory.instance.getClientSrvByPid(pid)
    if client == None:
        logging.warn(u"cmdCheckUser(), clientfactory.getClientSrvBySid() return None, pid=%d, uid=%d",pid,uid)
        return
    if code == Const.ERR_OK:
        session,nickname,photo,sex = packet.unpack("sssB",pkt,8)
        nickname = Utils.pstr2u(nickname)
        logging.debug("cmdCheckUserSuc() uid=%d, account=%s photo =%s",uid,nickname,photo)
        UserManager.instance.onCheckUserSuc(uid,session,sex,photo,nickname)
    else:
        Game.instance.onLoginGameResult(uid,code)

def cmdKickUser(center,pkt):
    uid,pid,code = packet.unpack("IHH",pkt)
    client = ClientFactory.instance.getClientSrvByUid(uid)
    if client and client.getId() == pid:
        UserManager.instance.kickUser(uid,code)
    else:
        logging.debug("center->game kick user error uid=%d pid=%d",uid,pid)
    pass

def cmdProp(center,pkt):
    uid,size = packet.unpack("IH", pkt)
    user = UserManager.instance.getUser(uid)
    if user:
        for i in range(size):
            head = i*6 + 6
            pid,pvalue = packet.unpack("IH", pkt, head)
            user.addProp(pid,pvalue)
    pass

def cmdProperty(center,pkt):
    uid,size = packet.unpack("IH", pkt)
    user = UserManager.instance.getUser(uid)
    if user:
        for i in range(size):
            head = i*6 + 6
            pid,pvalue = packet.unpack("IH", pkt, head)
            user.addProperty(pid,pvalue)
    pass

def cmdInfoEnd(center,pkt):
    uid = packet.unpack("I",pkt)
    Game.instance.onLoginGameResult(uid,Const.ERR_OK)

    pass

__cmdTable = {
        Const.CMD_KEEP_ALIVE                         : cmdKeepAlive,
        Const.CMD_CT2GAME_LOGINSUC                   : cmdLoginCenter,
        Const.CMD_CT2GAME_CHECK_USER                 : cmdCheckUser,
        Const.CMD_CT2GAME_KICK_USER                  : cmdKickUser,
        Const.CMD_CT2GAME_CREATE_TABLE               : cmdCreateTable,
        Const.CMD_CT2GAME_PROP                       : cmdProp,
        Const.CMD_CT2GAME_PROPERTY                   : cmdProperty,
        Const.CMD_CT2GAME_INFO_END                   : cmdInfoEnd

    }

def parse(center,cmd,pkt):
    if cmd == Const.CMD_KEEP_ALIVE:
        return
    func = __cmdTable.get(cmd)
    if not func:
        logging.error(u"cmd = %d not found",cmd);
    func(center,pkt)

if __name__ == "__main__":
    pass